package Siebel::Code::Analyzer::DAOFactory;

=pod

=head1 NAME

Siebel::Code::Analyzer::Repository::DAOFactory

=cut

use warnings;
use strict;
use MooseX::AbstractFactory;

=pod

=head1 SYNOPSIS

	use Siebel::Code::Analyzer::DAOFactory;

	my $dao = Siebel::Code::Analyzer::DAOFactory->create(
        $data_source,
        {
            data_source   => $data_source,
            user          => $user,
            password      => $password,
            max_long      => $maximum_long,
            repo_name     => $repository_name,
            last_upd      => $last_update, 
		    skip_inactive => $skip_inactive
        }
    );


=head1 DESCRIPTION

This is an abstract factory class to create instances of L<Siebel::Code::Analyzer::DAO> subclasses.

=head1 METHODS

=head2 create

Returns the instance of the class defined by the type given as parameter. Expects two parameters: an string with the type
of output and an hash reference with the parameters expected by the C<new> method of L<Siebel::Code::Analyzer::DAO>. 

=head2 can_create

Expects a string as the output type.

Returns true if there is a mapping between the given type and a subclass of 
otherwise it returns false;

=head1 SEE ALSO

=over

=item *

L<Siebel::Code::Analyzer::DAO>.

=back

=cut

our %table_mapping = (
    SQLite => 'Siebel::Code::Analyzer::DAO::SQLite',
    ODBC   => 'Siebel::Code::Analyzer::DAO::ODBC'
);

sub can_create {

    my $class       = shift;
    my $data_source = shift;

    #dbi:<DBD name>:something else

    my @data_source_attribs = split( /\:/, $data_source );
    die "Invalid data source string given: $data_source\n"
      unless ( defined( $data_source_attribs[1] ) );

    return ( exists( $table_mapping{ $data_source_attribs[1] } ) );

}

implementation_class_via sub {

    my $data_source = shift;
    my $object_data = shift;    # hash ref

    my @data_source_attribs = split( /\:/, $data_source );
    die "Invalid data source string given: $data_source\n"
      unless ( defined( $data_source_attribs[1] ) );

    if ( exists( $table_mapping{ $data_source_attribs[1] } ) ) {

        return $table_mapping{ $data_source_attribs[1] };

    }
    else {

        die "Cannot defined a class for data source $data_source";

    }

};

=pod

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.org<E<gt>

This project is based on the code (SiebelOVAnalyzer) provided in the article "Memory Leaks in 
Siebel" (http://siebelunleashed.com/memory-leaks-siebel/) from Timur Vafin. 

This file is part of Siebel Code Analyzer.

Siebel Code Analyzer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Siebel Code Analyzer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Siebel Monitoring Tools.  If not, see <http://www.gnu.org/licenses/>.

=cut

1;
